const {
  studentListService,
  insertStudentService,
  teacherListService,
  teacherToCourseService,
  addTeacherCourseService,
  editTeacherCourseService,
  delTeacherCourseService,
  studentScoreService,
  maxScoreService,
  menuRService,
  editMenuRoleService,
  menuService,
  insertMenuService,
  editMenuService,
  delMenuService,
  sdeptService,
  addSdeptService,
  editSdeptService,
  delSdeptService,
  classService,
  addClassService,
  editClassService,
  delClassService,
  studentCourseRelation,
  insertStudentCourese,
  roleService,
  addRoleService,
  addMenuRoleService,
  delMenuRoleService,
  delRoleService,
  addCourseService,
  delCourseService,
  editCourseService,
  courseService,
  getSdeptToCourseService,
  editSdeptToCourseService
} = require('@/service/school')
const { JsonWebTokenError, kewWordNotOnly } = require('@/constant/errType')
const { bcrpytPassword } = require('@/middleware/user')
const { createUser } = require('@/service/user')
const result = require('@/constant/result')

class SchoolController {
  // 获取角色
  async roleList(ctx,next) {
    try {
      const res = await roleService()
      ctx.body = result.success('获取角色成功', res)
      return ctx
    } catch(err) {
      console.error("获取角色失败")
    }
  }
  // 新增角色
  async addRole(ctx,next) {
    try {
      const data = ctx.request.body
      const res = await addRoleService(data)
      if (!data.isEdit) {
        await addMenuRoleService({ menu_id: [], role_id: data.level })
      }
      ctx.body = result.success('新增角色成功', res)
      return ctx
    } catch(err) {
      console.error("新增角色失败")
    }
  }
  async delRole(ctx,next) {
    try {
      const data = ctx.request.query
      const res = await delRoleService({_id: data._id})
      console.log(data,'===')
      await delMenuRoleService({role_id: data.level})
      ctx.body = result.success('删除角色成功', res)
      return ctx
    } catch(err) {
      console.error("删除角色失败")
    }
  }
  // 编辑角色菜单
  async editMenuRole(ctx, next) {
    try {
      const level = ctx.request.body
      const res = await editMenuRoleService(level)
      ctx.body = result.success('更新权限成功', res)
      return ctx
    } catch (err) {
      console.error("更新权限失败")
    }
  }
  // 获取角色菜单
  async menuRList(ctx, next) {
    try {
      const level = ctx.request.query
      const res = await menuRService(level.user_level)
      if (!level.isMange && res.length == 0){
        return ctx.app.emit('error', JsonWebTokenError, ctx)
      }
      ctx.body = result.success('获取菜单成功', res)
      return ctx
    } catch(err) {
      console.error("获取菜单失败")
    }
  }
  // 获取所有菜单
  async menuList(ctx, next) {
    try {
      const res = await menuService()
      ctx.body = result.success('获取菜单成功', res)
      return ctx
    } catch(err) {

    }
  }
  // 新建菜单
  async insertMenu(ctx, next) {
    try {
      const menu = ctx.request.body
      const res = await insertMenuService(menu)
      ctx.body = result.success('新建菜单成功', res)
      return ctx
    } catch(err) {
      if(err.code === 11000){
        ctx.app.emit('error', kewWordNotOnly, ctx)
      }
      console.error("新建菜单失败")
    }
  }
  // 编辑菜单
  async editMenu(ctx, next) {
    try {
      const menu = ctx.request.body
      const res = await editMenuService(menu)
      ctx.body = result.success('编辑菜单成功', res)
      return ctx
    } catch(err) {
      console.error("编辑菜单失败")
    }
  }
  // 删除菜单
  async delMenu(ctx, next) {
    try {
      const menu = ctx.request.query
      const res = await delMenuService(menu)
      ctx.body = result.success('删除菜单成功', res)
      return ctx
    } catch(err) {
      console.error("删除菜单失败")
    }
  }
  async studentList(ctx, next) {
    try {
      const class_name = ctx.request.query
      const res = await studentListService(class_name)
      ctx.body = result.success('获取学生列表成功', res)
      return ctx
    }catch(err){
      console.error("获取学生列表失败")
    }
  }
  async insertStudent(ctx, next) {
    try {
      const strdent = ctx.request.body
      ctx.request.body.password = '123456'
      const res = await insertStudentService(strdent)
      bcrpytPassword(ctx, next)
      await createUser(strdent.stu_name, ctx.request.body.password, 3)
      ctx.body = result.success('新建学生成功', res)
      return ctx
    }catch(err){
      console.error("新建学生失败")
    }
  }
  async teacherList(ctx, next) {
    try{
      const res = await teacherListService()
      ctx.body = result.success('获取老师列表成功', res)
      return ctx
    }catch(err) {
      console.log("获取老师列表失败")
    }
  }
  // 查询所有课程
  async courseList(ctx, next) {
    try {
      const res = await courseService()
      ctx.body = result.success('获取课程成功', res)
      return ctx
    } catch(err) {
      console.log("获取课程失败")
    }
  }
  // 新增课程
  async addCourse(ctx, next) {
    try {
      const data = ctx.request.body
      const res = await addCourseService(data)
      ctx.body = result.success('新增课程成功', res)
      return ctx
    } catch(err) {
      console.log("新增课程失败")
    }
  }
  // 编辑课程
  async editCourse(ctx, next) {
    try {
      const data = ctx.request.body
      const res = await editCourseService(data)
      ctx.body = result.success('编辑课程成功', res)
      return ctx
    } catch(err) {
      console.log("编辑课程失败")
    }
  }
  // 编辑课程
  async delCourse(ctx, next) {
    try {
      const data = ctx.request.query
      const res = await delCourseService(data)
      ctx.body = result.success('删除课程成功', res)
      return ctx
    } catch(err) {
      console.log("删除课程失败")
    }
  }
  // 每门课程对应的老师信息
  async teacherToCourseList(ctx, next) {
    try {
      const res = await teacherToCourseService()
      ctx.body = result.success('获取课程对应的老师信息成功', res)
      return ctx
    } catch(err) {
      console.log("获取课程对应的老师信息失败")
    }
  }
  // 新增老师课程
  async addTeacherCourse(ctx, next) {
    try {
      const data = ctx.request.body
      const res = await addTeacherCourseService(data)
      ctx.body = result.success('新增老师课程成功', res)
      return ctx
    } catch(err) {
      console.log("新增老师课程失败")
    }
  }
  // 新增老师课程
  async editTeacherCourse(ctx, next) {
    try {
      const data = ctx.request.body
      const res = await editTeacherCourseService(data)
      ctx.body = result.success('编辑老师课程成功', res)
      return ctx
    } catch(err) {
      console.log("编辑老师课程失败")
    }
  }
  // 删除老师课程
  async delTeacherCourse(ctx, next) {
    try {
      const data = ctx.request.query
      const res = await delTeacherCourseService(data)
      ctx.body = result.success('删除老师课程成功', res)
      return ctx
    } catch(err) {
      console.log("删除老师课程失败")
    }
  }
  // 获取学生成绩
  async studentScore(ctx,next) {
    try {
      let query = ctx.request.query
      const res = await studentScoreService(query)
      ctx.body = result.success('获取学生成绩成功', res)
      return ctx
    } catch(err) {
      console.log("获取学生成绩失败")
    }
  }
  // 查询最高分
  async maxScore(ctx,next) {
    try {
      const res = await maxScoreService()
      ctx.body = result.success('获取最高分成功', res)
      return ctx
    } catch(err) {
      console.log("获取最高分失败")
    }
  }
  // 查询院系
  async sdept(ctx,next) {
    try {
      const param = ctx.request.query
      const res = await sdeptService(param)
      ctx.body = result.success('获取院系成功', res)
      return ctx
    } catch(err) {
      console.log("获取院系失败")
    }
  }
  // 添加院系
  async addSdept(ctx, next) {
    try {
      const sdept = ctx.request.body
      const res = await addSdeptService(sdept)
      return ctx.body = result.success('添加院系成功', res)
    } catch(err) {
      console.log("添加院系失败")
    }
  }
  // 编辑院系
  async editSdept(ctx, next) {
    try {
      const {_id} = ctx.request.body
      const upSdept = ctx.request.body
      const res = await editSdeptService({_id}, upSdept)
      // const res = {}
      return ctx.body = result.success('编辑院系成功', res)
    } catch(err) {
      console.log("编辑院系失败")
    }
  }
  // 删除院系
  async delSdept(ctx, next) {
    try {
      const sdept = ctx.request.query
      const res = await delSdeptService(sdept)
      return ctx.body = result.success('删除院系成功', res)
    } catch(err) {
      console.log("删除院系失败")
    }
  }
  // 查询班级
  async classs(ctx,next) {
    try {
      const sdeptid = ctx.request.query
      const res = await classService(sdeptid)
      ctx.body = result.success('获取院系成功', res)
      return ctx
    } catch(err) {
      console.log("获取院系失败")
    }
  }
  // 添加班级
  async addClass(ctx, next) {
    try {
      const sdept = ctx.request.body
      const res = await addClassService(sdept)
      return ctx.body = result.success('添加院系成功', res)
    } catch(err) {
      console.log("添加院系失败")
    }
  }
  // 编辑班级
  async editClass(ctx, next) {
    try {
      const {_id} = ctx.request.body
      const sdept = ctx.request.body
      const res = await editClassService({_id}, sdept)
      return ctx.body = result.success('编辑院系成功', res)
    } catch(err) {
      console.log("编辑院系失败")
    }
  }
  // 删除班级
  async delClass(ctx, next) {
    try {
      const _id = ctx.request.query
      const res = await delClassService(_id)
      return ctx.body = result.success('删除院系成功', res)
    } catch(err) {
      console.log("删除院系失败")
    }
  }
  // 学生对应课程
  async studentCourse(ctx, next) {
    try {
      const res = await studentCourseRelation()
      return ctx.body = result.success("获取学生对应课程成功", res)
    } catch(err) {
      console.log("获取学生对应课程失败")
    }
  }
  // 添加学生对应课程
  async insertStudentCourse(ctx, next) {
    try {
      const scData = ctx.request.body
      const res = await insertStudentCourese(scData)
      return ctx.body = result.success("新建数据成功", res)
    } catch(err){
      console.log("新建数据失败")
    }
  }
  // 查询院系必须课
  async getSdeptToCourse(ctx, next) {
    try {
      const param = ctx.request.query
      const res = await getSdeptToCourseService(param)
      return ctx.body = result.success('查询院系课程成功', res)
    } catch(err){
      console.log('查询院系必修课失败')
    }
  }
  async editSdeptToCourse(ctx, next) {
    try {
      const param = ctx.request.body
      console.log(param)
      const res = await editSdeptToCourseService(param)
      return ctx.body = result.success('修改院系课程成功', res)
    } catch(err){
      console.log('修改院系必修课失败')
    }
  }
}

module.exports = new SchoolController()